package cn.com.inossem.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.inossem.activity.MyTEActivity;
import cn.com.inossem.activity.ProjectApplyExpenseActivity;
import cn.com.inossem.activity.ProjectApplyHourActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.model.TeExpenseItemBean;
import cn.com.inossem.model.TeHourItemBean;

/**
 * Created by Mickey.Li on 2017-8-9.
 */

public class TEListCommonAdapter<T> extends BaseAdapter {
	private String hourHtml = "总工时:<font color='#FDB118'> 0 </font>小时";
	private String incomeHtml = "总报销金额:<font color='#FDB118'> 0 </font>元";
	private Context mContext;
	private final LayoutInflater layoutInflater;
	private final int type;
	private List<T> teList = new ArrayList<>();
	private Intent intent;

	public TEListCommonAdapter(Context context, int type) {
		mContext = context;
		layoutInflater = LayoutInflater.from(context);
		this.type = type;
	}

	@Override
	public int getCount() {
		return teList.size();
	}

	@Override
	public T getItem(int position) {
		return teList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_telist_common, null);
			vh = new ViewHolder(convertView);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		int orderStatus;
		if (type == MyTEActivity.TYPE_TE_HOUR) {
			vh.itemTelistTotalm.setVisibility(View.GONE);
			if (TextUtils.isEmpty(((TeHourItemBean) getItem(position)).projectInfoUser.realProjectName)) {
				vh.itemTelistName.setText(((TeHourItemBean) getItem(position)).project.projectName);
			} else {
				vh.itemTelistName.setText(((TeHourItemBean) getItem(position)).projectInfoUser.realProjectName);
			}
			vh.itemTelistOrder.setText(mContext.getString(R.string.hour_no) + " " + ((TeHourItemBean) getItem(position)).no);
			vh.itemTelistTotalh.setText(Html.fromHtml(hourHtml.replace("0", "" + ((TeHourItemBean) getItem(position)).sumHours)));
			vh.itemTelistTime.setText(((TeHourItemBean) getItem(position)).createDate);
			orderStatus = ((TeHourItemBean) getItem(position)).status;
		} else {
			vh.itemTelistTotalh.setVisibility(View.GONE);
			if (TextUtils.isEmpty(((TeExpenseItemBean) getItem(position)).projectInfoUser.realProjectName)) {
				vh.itemTelistName.setText(((TeExpenseItemBean) getItem(position)).project.projectName);
			} else {
				vh.itemTelistName.setText(((TeExpenseItemBean) getItem(position)).projectInfoUser.realProjectName);
			}
			vh.itemTelistOrder.setText(mContext.getString(R.string.expense_no) + " " + ((TeExpenseItemBean) getItem(position)).no);
			vh.itemTelistTotalm.setText(Html.fromHtml(incomeHtml.replace("0", " " + ((TeExpenseItemBean) getItem(position)).sum)));
			vh.itemTelistTime.setText(((TeExpenseItemBean) getItem(position)).createDate);
			orderStatus = ((TeExpenseItemBean) getItem(position)).status;
		}
		vh.itemTelistStatus = (TextView) convertView.findViewById(R.id.item_telist_status);
		switch (orderStatus) {
			case 0:
				vh.itemTelistStatus.setText(mContext.getString(R.string.te_status_approve));
				vh.itemTelistStatus.setBackground(mContext.getResources().getDrawable(R.drawable.yanzhengmahuang));
				break;
			case 1:
				vh.itemTelistStatus.setText(mContext.getString(R.string.te_status_pass));
				vh.itemTelistStatus.setBackground(mContext.getResources().getDrawable(R.drawable.yanzhengmahui));
				break;
			case 2:
				vh.itemTelistStatus.setText(mContext.getString(R.string.te_status_unpass));
				vh.itemTelistStatus.setBackground(mContext.getResources().getDrawable(R.drawable.yishixiao));
				break;
			case 3:
				vh.itemTelistStatus.setText(mContext.getString(R.string.te_status_settle));
				vh.itemTelistStatus.setBackground(mContext.getResources().getDrawable(R.drawable.yihuode));
				break;
			case 4:
				vh.itemTelistStatus.setText(mContext.getString(R.string.te_status_save));
				vh.itemTelistStatus.setBackground(mContext.getResources().getDrawable(R.drawable.save));
				break;
			default:
				vh.itemTelistStatus.setText("");
				vh.itemTelistStatus.setBackground(null);
				break;
		}
		return convertView;
	}

	public void addData(List<T> mList, int pageNo) {
		if (pageNo == 1) {
			teList = new ArrayList<>();
		}
		teList.addAll(mList);
	}

	public void clearData() {
		teList = new ArrayList<>();
		notifyDataSetChanged();
	}

	/**
	 * 获取单号
	 */
	public Intent getApplyNo(int position) {
		if (type == MyTEActivity.TYPE_TE_HOUR) {
			intent = new Intent(mContext, ProjectApplyHourActivity.class);
			intent.putExtra("order_id", ((TeHourItemBean) getItem(position)).id);
			intent.putExtra("order_status", ((TeHourItemBean) getItem(position)).status);
			return intent;
		}
		intent = new Intent(mContext, ProjectApplyExpenseActivity.class);
		intent.putExtra("order_id", ((TeExpenseItemBean) getItem(position)).id);
		intent.putExtra("order_status", ((TeExpenseItemBean) getItem(position)).status);
		return intent;
	}

	class ViewHolder {
		@BindView(R.id.item_telist_time)
		TextView itemTelistTime;
		@BindView(R.id.item_telist_name)
		TextView itemTelistName;
		@BindView(R.id.item_telist_order)
		TextView itemTelistOrder;
		@BindView(R.id.item_telist_totalh)
		TextView itemTelistTotalh;
		@BindView(R.id.item_telist_status)
		TextView itemTelistStatus;
		@BindView(R.id.item_telist_ll)
		LinearLayout itemTelistLl;
		@BindView(R.id.item_telist_totalm)
		TextView itemTelistTotalm;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
